<template>
  <div class="user-data">
    <el-form :model="ruleForm" ref="ruleForm">
      <!-- <el-form-item label="用户名">
        <el-input v-model="ruleForm.name" placeholder="请输入用户名"></el-input>
      </el-form-item> -->
      <el-form-item label="邮箱">
		<el-input v-model="ruleForm.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="工作邮箱" v-if="ruleForm.role_id !==5">
        <el-input v-model="ruleForm.email_work" placeholder="请输入工作邮箱"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import aes from '~/util/aes.js'
export default {
  name: "userData",
  data() {
    return {
      ruleForm: {}
    };
  },
  created() {
    this.getUserInfo();
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    resetForm(formName) {
      this.getUserInfo();
    },
    getUserInfo() {
      this.ruleForm = this.$store.state.user;
    },
    //提交修改
    submitForm(){
        this.$axios.put('/api/consumer/update',{
            //name:this.ruleForm.name,
            email:this.ruleForm.email,
            email_work:this.ruleForm.email_work
        })
        .then(res => {
            if(res.data.code == 200){
                this.$toast('修改成功');
                //修改成功后更新本地存储的数据
                var obj = {};
                obj = this.$store.state.user;
                obj.name = this.ruleForm.name;
                obj.email = this.ruleForm.email;
                obj.email_work = this.ruleForm.email_work;
				this.$cookies.set('user',aes.Encrypt(JSON.stringify(obj)));
				this.$store.commit("setUser",obj);
				this.$router.replace({ path: "/user/myOrder" })
            }
        })
    }
  }
};
</script>
<style lang="less">
.user-data {
  .el-form {
    margin: 0 5%;
    width: 90%;
  }
}
</style>